<template>
    <div class="container">
        <div class="toptitle">
            <div class="titletext">智能巡逻</div>
            <hr />
        </div>
        <div class="content">
            <div class="map">
                <!-- 在左侧加载地图 -->
                <MAP :longitude="longitude" :latitude="latitude"  style="margin-left: 10px;width: 99%;height: 99%;" />
            </div>
            <div class="video">
                <!-- 在右侧插入视频 -->
                <video ref="videoPlayer" controls style="width: 100%; height: 100%; border-radius: 8px;">
                    <source src="https://m.hoire.cn/ai-srcs/video/无人机勘测识别作业.mp4" type="video/mp4">
                    您的浏览器不支持视频播放。
                </video>
            </div>
        </div>
        <div class="buttons">
            <!-- 两个按钮：规划路径和发送路径至机器 -->
            <button @click="planPath">规划路径</button>
            <button @click="sendPathToMachine">发送路径至机器</button>
        </div>

        <SuccessPopup :visible="showSuccessPopup1" @update:visible="showSuccessPopup1 = $event">
            正在规划路径...
        </SuccessPopup>
        <SuccessPopup :visible="showSuccessPopup2" @update:visible="showSuccessPopup2 = $event">
            发送路径成功
        </SuccessPopup>
    </div>
</template>

<script setup>
import MAP from '@/components/GPS/components/Satellite/index.vue'; // 引入地图组件
import { ref } from 'vue';
import useOperationStore from '../../../../../store/operation';
import SuccessPopup from '@/components/Popup/Success/index.vue'

let showSuccessPopup1 = ref(false);
let showSuccessPopup2 = ref(false);

// 切换业务的数据对接处理
let operationStore = useOperationStore();
let operationId = ref(operationStore.data.length > 0 ? operationStore.data[operationStore.index].id : '');
let latitude = ref(operationStore.data.length > 0 ? operationStore.data[operationStore.index].latitude : null);
let longitude = ref(operationStore.data.length > 0 ? operationStore.data[operationStore.index].longitude : null);
let operationName = ref(operationStore.data.length > 0 ? operationStore.data[operationStore.index].Name : null);
let videoPlayer = ref(null); // video player reference for controlling playback
let showPopup = ref(false); // Popup visibility

operationStore.$subscribe(async (mutation, state) => {
    operationId.value = operationStore.data[operationStore.index].id;
    operationName.value = operationStore.data.length > 0 ? operationStore.data[operationStore.index].Name : null;
    latitude.value = operationStore.data.length > 0 ? operationStore.data[operationStore.index].latitude : null;
    longitude.value = operationStore.data.length > 0 ? operationStore.data[operationStore.index].longitude : null;
});

// Define methods for button actions
const planPath = () => {
    
    showSuccessPopup1.value = true; // 显示弹窗
};

const sendPathToMachine = () => {
    // Logic for sending the path to the machine
    if (videoPlayer.value) {
        videoPlayer.value.play(); // Play the video after sending the path
    }

    showSuccessPopup2.value = true; // 显示弹窗
};
</script>

<style lang="scss" scoped>
.container {
    flex: 1;
    border-radius: 10px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;

    .toptitle {
        margin-top: 10px;
        margin-left: 20px;
        margin-right: 20px;

        .titletext {
            border-left: 3px solid var(--primary-color);
            padding-left: 20px;
        }

        hr {
            background-color: rgb(194, 194, 194);
            height: 1px;
            border: none;
        }
    }

    .content {
        display: flex;
        flex: 1;
        justify-content: space-between;

        .map {
            flex: 1;
            width: 50%;
            height: 100%;
            border-radius: 8px;
            position: relative;
        }

        .video {
            flex: 1;
            width: 50%;
            height: 100%;
            margin-left: 10px;
            border-radius: 5px;
        }
    }

    .buttons {
        display: flex;
        justify-content: space-around;
        margin-top: 5px;

        button {
            padding: 10px 20px;
            border-radius: 5px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s;

            &:hover {
                background-color: var(--Nav-hover-color);
            }
        }
    }

}
</style>
